package com.syq.impl;

import com.syq.bean.AirInfo;
import com.syq.dao.AirInfoDao;
import com.syq.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AirInfoImpl extends DBUtils implements AirInfoDao {
    @Override
    public List<AirInfo> getAllAirInfo() {
        AirInfo airInfo;
        List<AirInfo> list = new ArrayList<>();
        try {
            ResultSet resultSet = query("select * from airinfo",null);
            while (resultSet.next()) {
                airInfo = new AirInfo();
                airInfo.setId(resultSet.getInt("id"));
                airInfo.setAirId(resultSet.getString("airid"));
                airInfo.setDestination((resultSet.getString("destination")));
                airInfo.setBegintime(resultSet.getDate("begintime"));
                list.add(airInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return list;
    }

    @Override
    public List<AirInfo> findByTime(String time) {
        String sql = "select * from airinfo where begintime=?";
        List list = new ArrayList<>();
        list.add(time);
        List<AirInfo> airInfoList = new ArrayList<>();
        try {
            ResultSet resultSet = query(sql,list);
            while (resultSet.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(resultSet.getInt("id"));
                airInfo.setAirId(resultSet.getString("airid"));
                airInfo.setDestination((resultSet.getString("destination")));
                airInfo.setBegintime(resultSet.getDate("begintime"));
                airInfoList.add(airInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfoList;
    }

    @Override
    public List<AirInfo> findByDestination(String destination) {
        String sql = "select * from airinfo where destination like ?";
        List list = new ArrayList<>();
        list.add("%"+destination+"%");
        List<AirInfo> airInfoList = new ArrayList<>();
        try {
            ResultSet resultSet = query(sql,list);
            while (resultSet.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(resultSet.getInt("id"));
                airInfo.setAirId(resultSet.getString("airid"));
                airInfo.setDestination((resultSet.getString("destination")));
                airInfo.setBegintime(resultSet.getDate("begintime"));
                airInfoList.add(airInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfoList;
    }

    @Override
    public AirInfo findByAirId(String airId) {
        String sql = "select * from airinfo where airid=?";
        List list = new ArrayList<>();
        list.add(airId);
        AirInfo airInfo = new AirInfo();
        try {
            ResultSet resultSet = query(sql,list);
            while (resultSet.next()) {

                airInfo.setId(resultSet.getInt("id"));
                airInfo.setAirId(resultSet.getString("airid"));
                airInfo.setDestination((resultSet.getString("destination")));
                airInfo.setBegintime(resultSet.getDate("begintime"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfo;
    }

    @Override
    public int delete(String airId) {
        String sql = "delete from airinfo where airid=?";
        List list = new ArrayList<>();
        list.add(airId);
        int count = update(sql,list);
        return count;
    }

    @Override
    public int update(AirInfo airInfo) {
        String sql = "UPDATE airinfo SET destination=?,begintime=? where airid=?";
        List list = new ArrayList<>();
        list.add(airInfo.getDestination());
        list.add(airInfo.getBegintime());
        list.add(airInfo.getAirId());
        int count = update(sql,list);
        return count;
    }
}
